﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;

namespace GISCommon.Business
{
    public class TOCControlHelper
    {
        private TreeView treeView = null;
        public TreeView TREEVIEW
        {
            get { return treeView; }
            set { treeView = value;}
        }

        private AxMapControl mapControl = null;
        public AxMapControl MAPCONTROL
        {
            get { return mapControl; }
            set { mapControl = value; }
        }

        private RCITS_TOCControl tocControl = null;
        public RCITS_TOCControl TOCCONTROL
        {
            get { return tocControl; }
            set { tocControl = value; }
        }
        
        public void Initial()
        {
            if(treeView==null)
                treeView=new TreeView();

            treeView.Width = tocControl.Width;
            treeView.Height = tocControl.Height;
            treeView.Left = 0;
            treeView.Top = 0;
            treeView.Visible = true;
            tocControl.Controls.Add(treeView);
            tocControl.Controls[0].Anchor = tocControl.Anchor;
            tocControl.Controls[0].Dock = tocControl.Dock;
            AddTreeNodes();
            
            mapControl.OnExtentUpdated += new IMapControlEvents2_Ax_OnExtentUpdatedEventHandler(mapControl_OnExtentUpdated);
            treeView.AfterCheck += new TreeViewEventHandler(treeView_AfterCheck);
        }

        void treeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            CheckMapByNodes(e.Node);
        }

        void mapControl_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            CheckNodesByMap();
        }

        void CheckNodesByMap()
        {
            TreeNode rootNode = treeView.Nodes[0];
            int nodeCounts = rootNode.Nodes.Count;
            for(int i=0;i<nodeCounts;i++)
            {
                ILayer lyr = mapControl.get_Layer(i);
                TreeNode node = rootNode.Nodes[i];
                double lyrMinScale = lyr.MinimumScale;
                double lyrMaxScale = lyr.MaximumScale;
                double mapScale = mapControl.MapScale;

                //<for temp>
                if (lyr.Visible == false) node.Checked = false;
                //</for temp>

                if (lyrMaxScale <= mapScale && mapScale <= lyrMinScale && lyr.Visible==true)
                {
                    //打勾
                    node.Checked = true;
                }
                else
                {
                    //变为灰色勾

                    if (lyr.Visible)
                    {
                        //node.Checked = false;
                        
                    }

                }
                if (lyrMinScale == lyrMaxScale && lyr.Visible==true)
                    node.Checked = true;
            }
        }
        void CheckMapByNodes(TreeNode node)
        {
            int i = node.Index;
            
            ILayer lyr = mapControl.get_Layer(i);
            
            lyr.Visible = node.Checked;

            mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,lyr,null);
        }
        void AddTreeNodes()
        {
            treeView.Nodes.Add("图层控制");
            treeView.CheckBoxes = true;
            int lyrCount = mapControl.LayerCount;
            for(int i=0;i<lyrCount;i++)
            {
                ILayer lyr = mapControl.get_Layer(i);
                IFeatureLayer flyr =(IFeatureLayer) lyr;

                TreeNode node= treeView.Nodes[0].Nodes.Add(lyr.Name);
                double lyrMinScale = flyr.MinimumScale;
                double lyrMaxScale = flyr.MaximumScale;
                double mapScale = mapControl.MapScale;

                if (lyrMaxScale <= mapScale && mapScale <= lyrMinScale)
                {
                    node.Checked = true;
                }
                else
                {
                    if(lyr.Visible)
                    {
                        
                    }
                }
                if (lyrMinScale == lyrMaxScale)
                    node.Checked = true;
            }
            treeView.ExpandAll();
        }
        
    }
}
